package cn.hznc.controller.program;

import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.domain.entity.ProgramEntity;
import cn.hznc.domain.request.program.*;
import cn.hznc.domain.response.PageResult;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.ToolManageException;
import cn.hznc.controller.service.program.ProgramManageService;
import cn.hznc.permission.Permission;
import cn.hznc.utils.CommonUtil;
import cn.hznc.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;


/**
 * 主程序管理接口
 *
 * @Author Administrator
 * @Date 2023/3/13 16:00
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/program")
@Slf4j
public class ProgramManageController {
    @Autowired
    private ProgramManageService programManageService;

    /**
     * 主程序模板查询
     *
     * @param queryProgramReq querySubProgramReq
     * @return
     */
    @RequestMapping(value = "/queryProgramList", method = RequestMethod.POST)
    @Permission(title = "主程序列表查询" , permissions = {"program.read"})
    public ResponseBody<PageResult<ProgramEntity>> queryProgramList(@RequestBody QueryProgramReq queryProgramReq) {
        return ResponseBody.success(programManageService.queryProgramList(queryProgramReq));
    }

    /**
     * 新增或者修改主程序模板
     *
     * @param addOrEditProgramReq addOrEditProgramReq
     * @return
     */
    @RequestMapping(value = "/addOrEditProgram", method = RequestMethod.POST)
    @Log(title = "新增或编辑主程序信息" , businessType = BusinessType.OTHER)
    @Permission(title = "主程序列表新增或编辑" , permissions = {"program.add" , "program.update"})
    public ResponseBody<Boolean> addOrEditProgram(@RequestBody AddOrEditProgramReq addOrEditProgramReq) {
        return ResponseBody.success(programManageService.addOrEditProgram(addOrEditProgramReq));
    }

    /**
     * 删除主程序模板
     *
     * @param deleteProgramReq deleteProgramReq
     * @return
     */
    @RequestMapping(value = "/deleteProgram", method = RequestMethod.POST)
    @Log(title = "删除主程序信息" , businessType = BusinessType.DELETE)
    @Permission(title = "删除主程序信息" , permissions = {"program.delete"})
    public ResponseBody<Boolean> deleteProgram(@RequestBody DeleteProgramReq deleteProgramReq)  {
        try {
            programManageService.deleteProgram(deleteProgramReq);
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(),te.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 导入程序看板
     *
     * @return
     */
    @RequestMapping(value = "/importProgramBoard", method = RequestMethod.POST)
    @Log(title = "导入程序看板" , businessType = BusinessType.OTHER)
    public ResponseBody importProgramBoard(MultipartFile file)  {
        return ResponseBody.success(programManageService.importProgramBoard(file));
    }

    /**
     * 关联主程序和工艺工序
     *
     * @return
     */
    @RequestMapping(value = "/connectProgramAndCraft", method = RequestMethod.POST)
    @Log(title = "关联主程序和工艺工序" , businessType = BusinessType.OTHER)
    public ResponseBody connectProgramAndCraft(@RequestBody ConnectProAndCraftReq connectProAndCraftReq)  {
        try {
            return ResponseBody.success(programManageService.connectProgramAndCraft(connectProAndCraftReq));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode() , e.getMessage());
        }
    }

    /**
     * 程序使用时长首页
     *
     * @return
     */
    @RequestMapping(value = "/subProgramUsageReport", method = RequestMethod.POST)
    public ResponseBody subProgramUsageReport(@RequestBody SubprogramUsageReportReq subprogramUsageReportReq)  {
            return ResponseBody.success(programManageService.subProgramUsageReport(subprogramUsageReportReq));
    }

    /**
     * 程序使用时长首页
     *
     * @return
     */
    @RequestMapping(value = "/subProgramUsageReportDetail", method = RequestMethod.POST)
    public ResponseBody subProgramUsageReportDetail(@RequestBody SubprogramUsageReportReq subprogramUsageReportReq)  {
        return ResponseBody.success(programManageService.subProgramUsageReportDetail(subprogramUsageReportReq));
    }

    /**
     * 程序实际使用时长统计首页
     *
     * @return
     */
    @RequestMapping(value = "/programUsageIndex", method = RequestMethod.POST)
    public ResponseBody programUsageIndex(@RequestBody ProgramUsageIndex programUsageIndex)  {
        return ResponseBody.success(programManageService.programUsageIndex(programUsageIndex));
    }

    /**
     * 程序实际使用时长统计首页
     *
     * @return
     */
    @RequestMapping(value = "/programUsageGroup", method = RequestMethod.POST)
    public ResponseBody programUsageGroup(@RequestBody ProgramUsageGroupReq programUsageIndex)  {
        return ResponseBody.success(programManageService.programUsageGroup(programUsageIndex));
    }

    /**
     * 导出程序excel模版
     *
     * @param response
     */
    @RequestMapping(value = "/exportProgramTemplate", method = RequestMethod.GET)
    public void exportProgramTemplate(HttpServletResponse response) {
        programManageService.exportProgramTemplate(response);
    }

    /**
     * 导入产品excel模版
     */
    @RequestMapping(value = "/importProgramTemplate", method = RequestMethod.POST)
    public ResponseBody importProgramTemplate(@RequestPart("file") MultipartFile file) {
//        List<String> batchadd = FileUtils.batchadd(file);
//        if (CollectionUtil.isNotEmpty(batchadd)) {
//            batchadd.forEach(s -> {
//                ProductionReadListener easyExcelListener = new ProductionReadListener();
//                ExcelReader excelReader = null;
//                try {
//                    excelReader = EasyExcelFactory.read(new FileInputStream(s), easyExcelListener).build();
//                    List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
//                    Map<Integer, List<LinkedHashMap>> sheetInfos = new HashMap<>(sheets.size());
//                    for (ReadSheet sheet : sheets) {
//                        Integer sheetNo = sheet.getSheetNo();
//                        excelReader.read(sheet);
//                        sheetInfos.put(sheetNo, easyExcelListener.getListMap());
//                    }
//                    //最终数据
//                    List<LinkedHashMap> mapList = sheetInfos.get(0);
//                    programManageService.importProgramTemplate(mapList);
//                } catch (IOException e) {
//                    log.error("10001", "解析失败");
//                }
//            });
//        }
        List<List<LinkedHashMap>> dataList = CommonUtil.uploadZipFile(file);
        programManageService.importProgramTemplate(dataList);
        return ResponseBody.success();
    }


}
